﻿//This code was taken from http://msdn.microsoft.com/en-us/library/windows/apps/br211380.aspx

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.ApplicationSettings;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;

// The Blank Page item template is documented at http://go.microsoft.com/fwlink/?LinkId=234238

namespace SoCalNews
{
    /// <summary>
    /// An empty page that can be used on its own or navigated to within a Frame.
    /// </summary>
    public sealed partial class MainPage : Page
    {
        public MainPage()
        {
            this.InitializeComponent();
            SettingsPane.GetForCurrentView().CommandsRequested += ShowPrivacyPolicy;  
        }
         

            // Method to add the privacy policy to the settings charm
            private void ShowPrivacyPolicy(SettingsPane sender, SettingsPaneCommandsRequestedEventArgs args)
            {
                SettingsCommand privacyPolicyCommand = new SettingsCommand("privacyPolicy","Privacy Policy", (uiCommand) => { LaunchPrivacyPolicyUrl(); });
                args.Request.ApplicationCommands.Add(privacyPolicyCommand);
            }

            // Method to launch the url of the privacy policy
            async void LaunchPrivacyPolicyUrl()
            {
                Uri privacyPolicyUrl = new Uri("http://www.yoursite.com/privacypolicy");
                var result = await Windows.System.Launcher.LaunchUriAsync(privacyPolicyUrl);
            }
        int currentSource = 0;

        /// <summary>
        /// Invoked when this page is about to be displayed in a Frame.
        /// </summary>
        /// <param name="e">Event data that describes how this page was reached.  The Parameter
        /// property is typically used to configure the page.</param>
        protected override async void OnNavigatedTo(NavigationEventArgs e)
        {
            FeedDataSource feedDataSource = (FeedDataSource)App.Current.Resources["feedDataSource"];
            if (currentSource == 0) {
                Previous.Visibility = Windows.UI.Xaml.Visibility.Collapsed;
            }
            else if (feedDataSource.Feeds.Count - 1 <= currentSource) {
                Next.Visibility = Windows.UI.Xaml.Visibility.Collapsed;
            }
            if (feedDataSource != null)
            {
                if (feedDataSource.Feeds.Count == 0)
                {
                    await feedDataSource.GetFeedsAsync();
                }
                this.DataContext = (feedDataSource.Feeds)[currentSource];
            }
        }

        private void ItemListView_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            // If there's a selected item (in AddedItems)
            // show it in the WebView.
            if (e.AddedItems.Count > 0)
            {
                FeedItem feedItem = e.AddedItems[0] as FeedItem;
                if (feedItem != null)
                {
                    // Navigate the WebView to the blog post content HTML string.
                    //ContentView.NavigateToString(feedItem.Content);
                    ContentView.Visibility = Windows.UI.Xaml.Visibility.Collapsed;
                    BlockingRect.Visibility = Windows.UI.Xaml.Visibility.Visible;
                    ProgressRing1.IsActive = true;
                    ContentView.LoadCompleted += new Windows.UI.Xaml.Navigation.LoadCompletedEventHandler(WebView1_LoadCompleted);
                    var itemLink = feedItem.Link.ToString();
                    Uri targetUri = new Uri(itemLink);
                    ContentView.Navigate(targetUri);
                }
            }
            else
            {
                // If the item was de-selected, clear the WebView.
                ContentView.NavigateToString("");
            }
        }
        void WebView1_LoadCompleted(object sender, Windows.UI.Xaml.Navigation.NavigationEventArgs e)
        {
            ContentView.Visibility = Windows.UI.Xaml.Visibility.Visible;
            BlockingRect.Visibility = Windows.UI.Xaml.Visibility.Collapsed;
            ProgressRing1.IsActive = false;

            // Tell the user that the page has loaded
        }

        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            FeedDataSource feedDataSource = (FeedDataSource)App.Current.Resources["feedDataSource"];
            
            if (feedDataSource.Feeds.Count - 1 > currentSource)
            {
                currentSource += 1;
                this.DataContext = (feedDataSource.Feeds)[currentSource];
                if (currentSource == 0)
                {
                    Previous.Visibility = Windows.UI.Xaml.Visibility.Collapsed;
                    Next.Visibility = Windows.UI.Xaml.Visibility.Visible;
                }
                else if (feedDataSource.Feeds.Count - 1 == currentSource)
                {
                    Next.Visibility = Windows.UI.Xaml.Visibility.Collapsed;
                    Previous.Visibility = Windows.UI.Xaml.Visibility.Visible;
                }
                else {
                    Previous.Visibility = Windows.UI.Xaml.Visibility.Visible;
                    Next.Visibility = Windows.UI.Xaml.Visibility.Visible;
                }
            }
            
            
        }
        private void Button_Click_2(object sender, RoutedEventArgs e)
        {
            FeedDataSource feedDataSource = (FeedDataSource)App.Current.Resources["feedDataSource"];

            if (currentSource > 0)
            {
                currentSource -= 1;
                this.DataContext = (feedDataSource.Feeds)[currentSource];
                if (currentSource == 0)
                {
                    Previous.Visibility = Windows.UI.Xaml.Visibility.Collapsed;
                    Next.Visibility = Windows.UI.Xaml.Visibility.Visible;
                }
                else if (feedDataSource.Feeds.Count - 1 <= currentSource)
                {
                    Next.Visibility = Windows.UI.Xaml.Visibility.Collapsed;
                }
                else {
                    Previous.Visibility = Windows.UI.Xaml.Visibility.Visible;
                    Next.Visibility = Windows.UI.Xaml.Visibility.Visible;
                }
            }
        }
    }
}
